<template>
  <div class="form">
    <el-dialog
      :visible.sync="dialogFormVisible"
      :title="type == 2 ? '修改平台' : '添加平台'"
      @close="cancel"
    >
      <el-form style="width: 80%" :model="params" :rules="rules" ref="formRef">
        <el-form-item label="平台名称" label-width="100px" prop="spread_name">
          <el-input
            placeholder="请输入平台名称"
            v-model="params.spread_name"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号" label-width="100px" prop="tel">
          <el-input
            type="number"
            placeholder="请输入手机号"
            v-model="params.tel"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="100px" prop="password">
          <el-input
            type="password"
            placeholder="请输入密码"
            v-model="params.password"
            size="small"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="state" label-width="100px">
          <el-radio v-model="params.state" :label="1">启用</el-radio>
          <el-radio v-model="params.state" :label="2">禁用</el-radio>
        </el-form-item>
      </el-form>
      <!-- 具名插槽 -->
      <template #footer>
        <el-button @click="cancel" size="mini">取消</el-button>
        <el-button type="primary" @click="submit" size="mini">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { create, update } from "@/api/plat"
export default {
  name: "my-form",
  data() {
    return {
      type: 1,
      dialogFormVisible: false,
      params: {
        spread_name: "",
        tel: "",
        state: 1,
        password: "",
      },
      rules: {
        spread_name: [
          { required: true, message: "请输入平台名称", trigger: "blur" },
        ],
        tel: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          { min: 11, max: 11, message: "请输入正确的手机号", trigger: "blur" },
        ],
        state: [{ required: true, message: "请选择状态", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    }
  },
  methods: {
    show(type = 1, row) {
      this.type = type
      this.reset()
      this.dialogFormVisible = true
      if (type == 2) {
        const { spread_name, tel, state, spread_id } = row
        this.params = {
          ...this.params, // 保留已有属性
          spread_name,
          tel,
          state,
          spread_id,
        }
      }
    },
    reset() {
      this.params = {
        spread_name: "",
        tel: "",
        state: 1,
        password: "",
      }
      this.$nextTick(() => {
        this.$refs.formRef.clearValidate()
      })
    },
    // 取消
    cancel() {
      this.reset()
      this.dialogFormVisible = false
    },
    // 提交
    submit() {
      this.$refs.formRef.validate(async (valid) => {
        if (valid) {
          let url = this.type === 1 ? create(this.params) : update(this.params)
          try {
            const res = await url
            this.$msg.successMsg(res.msg)
            this.cancel()
            this.$emit("refresh")
          } catch (ex) {
            console.log(ex)
          }
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped></style>
